/**
 * 
 */
package com.vladimirjosu.fhc;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;

import com.vladimirjosu.util.Util;

/**
 * @author vjosu
 * 
 */
public class Problem {

	private List<String> mInput;
	private String mOutput;
	private int mLines;
	private Class<?> mClass;

	/**
	 * 
	 */
	public Problem() {
	}

	public Problem(String input, String output, Class<?> c, int lines) {
		mInput = Util.readInput(input);
		mOutput = Util.getUserDirectory("output/" + output);
		mLines = lines;
		mClass = c;

	}

	public String solve() throws InstantiationException,
			IllegalAccessException, NoSuchMethodException, SecurityException,
			IllegalArgumentException, InvocationTargetException {
		Util.write(mOutput, "", false);
		String result = null;
		Constructor<?> constructor;
		Object problem = null;
		for (int i = 1; i < mInput.size(); i++) {
			switch (mLines) {
			case 1:
				constructor = mClass.getConstructor(String.class);
				problem = constructor.newInstance(mInput.get(i));
				break;
			case 2:
				constructor = mClass.getConstructor(String.class, String.class);
				problem = constructor.newInstance(mInput.get(i),
						mInput.get(++i));
				break;
			case 3:
				constructor = mClass.getConstructor(String.class, String.class,
						String.class);
				problem = constructor.newInstance(mInput.get(i),
						mInput.get(++i), mInput.get(++i));
				break;

			default:
				break;
			}
			Method method = mClass.getMethod("solve");
			result = (String) method.invoke(problem);
			String output = "Case #" + i / mLines + ": " + result + "\n";
			Util.write(mOutput, output, true);
			Util.log(output);
		}
		return null;

	}

	/**
	 * @param args
	 * @throws InvocationTargetException
	 * @throws IllegalArgumentException
	 * @throws SecurityException
	 * @throws NoSuchMethodException
	 * @throws IllegalAccessException
	 * @throws InstantiationException
	 */
	public static void main(String[] args) throws InstantiationException,
			IllegalAccessException, NoSuchMethodException, SecurityException,
			IllegalArgumentException, InvocationTargetException {
		// new Problem("card_game.txt", "card_game.txt", CardGame.class, 2)
		// .solve();
		// new Problem("input.txt", "card_game.txt", CardGame.class, 2).solve();
		// new Problem("beautiful_strings.txt", "beautiful_strings.txt",
		// BeautifulStrings.class, 1).solve();
		// new Problem("balanced_smileys.txt", "balanced_smileys.txt",
		// BalancedSmileys.class, 1).solve();
		// new Problem("dead_pixels.txt", "dead_pixels.txt", DeadPixels.class,
		// 1)
		// .solve();
		new Problem("security.txt", "security.txt", Security.class, 3).solve();
	}

}
